package com.sfzd5.UI.answersheet;

import com.sfzd5.exam.helplibrary.exam.answersheet.BlankInfo;

import javax.swing.*;
import java.awt.*;

public class BlankEditBar extends AnswerSheetQuestionEditBar {
    JTextField tf_answer;
    JSpinner sp_score;
    JSpinner sp_width;

    BlankInfo blankInfo;
    public BlankEditBar(BlankInfo blankInfo){
        super();

        this.blankInfo = blankInfo;

        JLabel lb_name = new JLabel("答案");
        this.add(lb_name);

        tf_answer = new JTextField(this.blankInfo.answer);
        setTextFieldSize(tf_answer);
        this.add(tf_answer);

        JLabel lb_width = new JLabel("宽度");
        this.add(lb_width);

        sp_width = new JSpinner();
        SpinnerNumberModel widthSpinnerNumberModel = new SpinnerNumberModel();
        widthSpinnerNumberModel.setMinimum(2);
        widthSpinnerNumberModel.setMaximum(500);
        widthSpinnerNumberModel.setStepSize(1);
        sp_width.setModel(widthSpinnerNumberModel);
        sp_width.setValue(this.blankInfo.width);
        this.add(sp_width);


        JLabel lb_score = new JLabel("分值");
        this.add(lb_score);

        sp_score = new JSpinner();
        SpinnerNumberModel spinnerNumberModel = new SpinnerNumberModel();
        spinnerNumberModel.setMinimum(1);
        spinnerNumberModel.setMaximum(100);
        spinnerNumberModel.setStepSize(1);
        sp_score.setModel(spinnerNumberModel);
        sp_score.setValue(this.blankInfo.score);
        this.add(sp_score);
    }

    private void setTextFieldSize(JTextField tf){
        Dimension d = tf.getSize();
        d.width = 50;
        d.height = 22;
        tf.setSize(d);
        tf.setMaximumSize(d);
        tf.setMinimumSize(d);
        tf.setPreferredSize(d);
    }

    public BlankInfo getBlankInfo(){
        this.blankInfo.answer = tf_answer.getText().trim().toUpperCase();
        this.blankInfo.width = (int) sp_width.getValue();
        this.blankInfo.score = (int) sp_score.getValue();
        return this.blankInfo;
    }
}
